﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuantitativeInvestment.Bean;
using QuantitativeInvestment.Tools;

namespace QuantitativeInvestment.Factor
{
    class BbandsFactor:Factor
    {
        public BbandsFactor()
        {
            this.name = "布林通道";
            Parameter p1 = new Parameter("天数",30);
            this.paraList.Add(p1.name,p1);

            Parameter p2 = new Parameter("InNbDevUp", 0.02);
            this.paraList.Add(p2.name, p2);

            Parameter p3 = new Parameter("InNbDevDn", 2);
            this.paraList.Add(p3.name, p3);

        }
        public override void addFactorValue(Stock stock)
        {
            int num = Int32.Parse(this.paraList["天数"].value.ToString());
            double optInNbDevUp = Double.Parse(this.paraList["InNbDevUp"].value.ToString());
            double optInNbDevDn = Double.Parse(this.paraList["InNbDevDn"].value.ToString());

            if (!stock.factors.ContainsKey(this.name + this.paraList["天数"].value.ToString()))
            {
                TaLib lib = new TaLib();

                double[,] bbandsValues = lib.getBbands(stock.factors["价格"], num, optInNbDevUp, optInNbDevDn);
                int length=stock.factors["价格"].Length;
                double[] result1 = new double[length];
                double[] result2 = new double[length];
                double[] result3 = new double[length];

                for (int i = 0; i < length; i++)
                {
                    result1[i] = bbandsValues[0, i];
                    result2[i] = bbandsValues[1, i];
                    result3[i] = bbandsValues[2, i];
                }

                stock.factors.Add(this.name + this.paraList["天数"].value.ToString() + "RealUpperBand", result1);
                stock.factors.Add(this.name + this.paraList["天数"].value.ToString() + "RealMiddleBand", result2);
                stock.factors.Add(this.name + this.paraList["天数"].value.ToString() + "RealLowerBand", result3);

            }
        }
    }
}
